package com.jiedeng.util.httpclient;

import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * http,https请求工具类
 *
 * @author wym
 * @date 2018/3/15
 */

public class HttpClientHelper {

  private final static Logger logger = LoggerFactory.getLogger(HttpClientHelper.class);

  /**
   * 请求超时时间设置
   */
  private final static int SOCKET_TIMEOUT = 5000;
  private final static int CONNECT_TIMEOUT = 5000;

  /**
   * 请求提交类型
   */
  public enum CONTENT_TYPE {
    APPLICATION_FORM_URLENCODED("application/x-www-form-urlencoded"),
    APPLICATION_JSON("application/json"),
    TEXT_XML("text/xml");
    private String value;

    CONTENT_TYPE(String value) {
      this.value = value;
    }

    public String getValue() {
      return value;
    }

    public void setValue(String value) {
      this.value = value;
    }
  }

  /**
   * get请求
   * @param url 请求路径
   * @return
   */
  public static String getRequest(String url) throws Exception {
    return getRequest(url, null);
  }

  /**
   * get请求
   * @param url 请求路径
   * @param headersParams  请求头参数
   * @return
   * @throws Exception
   */
  public static String getRequest(String url, Map<String, String> headersParams) throws Exception {
    CloseableHttpClient httpClient = SSLUtils.createSSLInsecureClient();
    return doGet(httpClient, url, headersParams);
  }

  /**
   * post请求
   * @param url  请求路径
   * @param dataBody  传递参数
   * @return
   */
  public static String postRequest(String url, String dataBody) throws Exception {
    return postRequest(url, dataBody, null, null);
  }


  /**
   * post请求，指定contentType
   * @param url  请求路径
   * @param dataBody  传递参数
   * @param contentType   请求类型（不指定传null）
   * @param headersParams 自定义请求头参数（不需要传null）
   * @return
   */
  public static String postRequest(String url, String dataBody, CONTENT_TYPE contentType,
      Map<String, String> headersParams)
      throws Exception {
    CloseableHttpClient httpClient = SSLUtils.createSSLInsecureClient();
    return doPost(httpClient, url, dataBody, contentType.getValue(), headersParams);
  }
  /**
   * post请求，指定contentType
   * @param url  请求路径
   * @param dataBody  传递参数
   * @param contentType   请求类型（不指定传null）
   * @param headersParams 自定义请求头参数（不需要传null）
   * @return
   */
  public static String postRequest2(String url, Map<String, String> dataBody, String contentType,
       String headersParams)
      throws Exception {
    return doPost( url, dataBody, contentType, headersParams);
  }


  /**
   * get请求
   * @param httpClient
   * @param url
   * @param headersParams
   * @return
   * @throws Exception
   */
  private static String doGet(CloseableHttpClient httpClient, String url,
      Map<String, String> headersParams) throws Exception {
    HttpGet get = new HttpGet(new URI(url));
    //超时设置
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SOCKET_TIMEOUT)
        .setConnectTimeout(CONNECT_TIMEOUT).build();//设置请求和传输超时时间
    get.setConfig(requestConfig);

    if (headersParams != null) {
      for (Map.Entry<String, String> enks : headersParams.entrySet()) {
        get.addHeader(enks.getKey(), enks.getValue());
      }
    }

    HttpResponse response = httpClient.execute(get);
    HttpEntity entity = response.getEntity();
    String result = EntityUtils.toString(entity, "UTF-8");
    return result;
  }
  /**
   * get请求
   * @return
   */
  public static String doGet(String url) {
    try {
      HttpClient client = new DefaultHttpClient();
      //发送get请求
      HttpGet request = new HttpGet(url);
      HttpResponse response = client.execute(request);

      /**请求发送成功，并得到响应**/
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        /**读取服务器返回过来的json字符串数据**/
        String strResult = EntityUtils.toString(response.getEntity());

        return strResult;
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }

    return null;
  }
  /**
   * post请求
   * @param httpClient
   * @param url
   * @param dataBody
   * @param contentType
   * @param headersParams 自定义请求头参数
   * @return
   * @throws Exception
   */
  private static String doPost(CloseableHttpClient httpClient, String url, String dataBody,
      String contentType, Map<String, String> headersParams) throws Exception {

    if (dataBody == null) {
      dataBody = "";
    }

    HttpPost httpPost = new HttpPost(new URI(url));
    //超时设置
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SOCKET_TIMEOUT)
        .setConnectTimeout(CONNECT_TIMEOUT).build();//设置请求和传输超时时间
    httpPost.setConfig(requestConfig);

    if (contentType != null) {
      Header header = new BasicHeader("Content-Type", contentType);
      httpPost.addHeader(header);
    }
    //设置自定义请求头参数
    if (headersParams != null && headersParams.size() > 0) {
      for (Map.Entry<String, String> enks : headersParams.entrySet()) {
        httpPost.addHeader(enks.getKey(), enks.getValue());
      }
    }

    httpPost.setEntity(new StringEntity(dataBody, "UTF-8"));
    HttpResponse response = httpClient.execute(httpPost);

    HttpEntity entity = response.getEntity();
    String responseContent = "";
    if (null != entity) {
      responseContent = EntityUtils.toString(entity, "UTF-8");
      EntityUtils.consume(entity);
    }
    return responseContent;
  }
  public static String doPost(String url, Map<String,String> map, String charset, String auth){
    HttpClient httpClient = null;
    HttpPost httpPost = null;
    String result = null;
    try{
      httpClient = new SSLClient();
      httpPost = new HttpPost(url);
      //设置参数
      httpPost.setHeader("Authorization", auth);
      List<NameValuePair> list = new ArrayList<NameValuePair>();
      Iterator iterator = map.entrySet().iterator();
      while(iterator.hasNext()){
        Map.Entry<String,String> elem = (Map.Entry<String, String>) iterator.next();
        list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
      }
      if(list.size() > 0){
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
        httpPost.setEntity(entity);
      }
      HttpResponse response = httpClient.execute(httpPost);
      if(response != null){
        HttpEntity resEntity = response.getEntity();
        if(resEntity != null){
          result = EntityUtils.toString(resEntity,charset);
        }
      }
    }catch(Exception ex){
      ex.printStackTrace();
    }
    return result;
  }

}